package com.rbs.controller;

import com.rbs.entity.Address;
import com.rbs.entity.User;
import com.rbs.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("/selectByUserId")
    public String selectByUserId(int state,HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        System.out.println(userId);
        List<Address> addresses = addressService.selectByUserId(userId);
        model.addAttribute("addresses",addresses);
        if (state==1){
            //跳转到展示收货地址页面
            return "forward:/address/my_address.jsp";
        }else {
            //购物车选择地址页面
            return "forward:/order/address_form.jsp";
        }

    }
    @RequestMapping("/deleteById")
    public String deleteById(int addressId){
        addressService.deleteById(addressId);
        return "redirect:/address/selectByUserId.do?state=1";
    }
    @RequestMapping("/selectedState")
    public String selectedState(int addressId,HttpSession session){
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        addressService.updateSelectedStateToNull(userId);
        addressService.updateSelectedStateToSelectedState(addressId);
        return "redirect:/address/selectByUserId.do?state=1";
    }
}
